package org.itli.jnsocket.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.itli.jnsocket.Pojo.Customer;
import org.itli.jnsocket.Pojo.WareHouse;
import org.itli.jnsocket.Result.Result;
import org.itli.jnsocket.Service.WareHouseService;
import org.itli.jnsocket.Util.JWTUtil;
import org.itli.jnsocket.Util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Tag(name = "仓库接口")
@RequestMapping("/Warehouse")
@RestController
public class WareHouseController {

    WareHouseService service;

    JWTUtil jwt = new JWTUtil();

    @Autowired
    public void setService(WareHouseService service) {
        this.service = service;
    }

    //获取全部仓库
    @PostMapping("/AllWH")
    @Operation(summary = "获取全部仓库")
    public Result<String> AllWH(@RequestBody PageUtil page){
        log.info("allwh 被调用");
        String s = service.AllWH(page);
        return Result.success(s);
    }

    //新增仓库
    @PostMapping("/AddWareHouse")
    @Operation(summary = "添加仓库")
    public Result<String> AddWH(@RequestHeader("Authorization") String token, @RequestBody WareHouse wareHouse){
        Map<String, Object> cliam;
        try{
            cliam = jwt.parseToken(token);
        } catch (Exception e) {
            return Result.success("令牌过期");
        }
        String username = (String) cliam.get("username");
        String s = service.AddWH(wareHouse, username);
        return Result.success(s);
    }

    //修改仓库
    @PostMapping("/UpdateHouse")
    @Operation(summary = "修改仓库")
    public Result<String> UpdateWH(@RequestHeader("Authorization") String token, @RequestBody WareHouse wareHouse){
        Map<String, Object> cliam;
        try{
            cliam = jwt.parseToken(token);
        } catch (Exception e) {
            return Result.success("令牌过期");
        }
        String username = (String) cliam.get("username");
        String s = service.UpdateWH(wareHouse, username);
        return Result.success(s);
    }

    //删除仓库
    @PostMapping("/DeleteWarehouse")
    @Operation(summary = "删除仓库")
    public Result<String> DeleteWH(@RequestHeader("Authorization") String token, @RequestBody WareHouse wareHouse){
        Map<String, Object> cliam;
        try{
            cliam = jwt.parseToken(token);
        } catch (Exception e) {
            return Result.success("令牌过期");
        }
        String username = (String) cliam.get("username");
        String s = service.DeleteWH(wareHouse, username);
        return Result.success(s);
    }
}
